package com.taekwon.www.remote.dao;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.taekwon.www.remote.model.FxBoard;

public class DaoForIBatis {
	private static SqlMapClient sqlMapClient;
	static{
		try{
			Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
			sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
			reader.close();
			
		}catch(IOException e){
			
		}
	}
	
	public ArrayList<FxBoard> selectFx() throws SQLException{
		return (ArrayList<FxBoard>) sqlMapClient.queryForList("selectFx");
	}
	public int getPageCount(String keyword) throws NumberFormatException, SQLException{
		return Integer.parseInt(sqlMapClient.queryForObject("getPageCount",keyword).toString());
	}
	public ArrayList<FxBoard> selectPaging(int page,String keyword) throws SQLException{
		HashMap map = new HashMap();
		map.put("page", page);
		map.put("keyword", keyword);
		return (ArrayList<FxBoard>) sqlMapClient.queryForList("selectPaging",map);
	}
	public int deleteFx(int fxseq) throws SQLException{
		return sqlMapClient.delete("deleteFx",fxseq);
	}
	public int insertFx(FxBoard fb) throws SQLException{
		return sqlMapClient.update("insertFx", fb);
	}
	public int updateFx(FxBoard fb) throws SQLException{
		return sqlMapClient.update("updateFx", fb);
	}
	public static void main(String[] args) throws SQLException {
		DaoForIBatis dfib = new DaoForIBatis();
//		FxBoard fb = new FxBoard();
//		fb.setFxcontent("��þ�");
//		fb.setFxtitle("�׳�");
//		fb.setFxwriter("���±�1");
//		System.out.println(dfib.insertFx(fb));
	
		
		ArrayList<FxBoard> list = dfib.selectPaging(0, "");
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i).getFxseq());
		}
		System.out.println(dfib.getPageCount("����"));
		
//		FxBoard fb = new FxBoard();
//		fb.setFxcontent("��þ�");
//		fb.setFxtitle("�׳�");
//		fb.setFxwriter("���±�1");
//		fb.setFxseq(171);
//		int appliedNum = dfib.updateFx(fb);
//		System.out.println(appliedNum);
//		System.out.println(dfib.deleteFx(172));
//		ArrayList<FxBoard> fbs = dfib.selectFx();
//		System.out.println(fbs.size());
//		for( FxBoard fv :fbs ){
//			System.out.println(fv.getFxseq());
//		}
	}
	
}










